package com.sinoglobal.wallet.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sinoglobal.wallet.util.LogUtils;

/**
 * 
 * @author lgy
 * @createdate 2015-2-12 下午4:49:53
 * @Description: (用一句话描述该类做什么)时间工具类
 */
public class TimeUtils {

	/**
	 * 获取手机当前时间的毫秒值
	 * 
	 * @return
	 */
	public static long getCurrentTimeInLong() {
		return System.currentTimeMillis();
	}

	/**
	 * 
	 * @author lgy
	 * @createdate 2015-2-13 下午4:10:27
	 * @Description: (用一句话描述该方法做什么) 获取当天日期
	 * @param dateformat
	 *            格式 如 yyyy-MM-dd
	 * @return
	 * 
	 */
	public String getNowTime(String dateformat) {
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 可以方便地修改日期格式
		String hehe = dateFormat.format(now);
		return hehe;
	}

	/**
	 * 
	 * @author zhourihu
	 * @createdate 2013-12-24 下午2:23:43
	 * @Description: (用一句话描述该方法做什么) 将时间戳转换为 模糊时间 其实一般工具类里对异常不做处理，交给调用者处理
	 *            需要转换的时间戳
	 * @param create_time
	 * @return
	 * 
	 */
	public static String parseTimestampToString(String create_time) {
		long during = 0;
		try {
			during = Long.parseLong(create_time);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return "不久前";
		}
		long nowL = System.currentTimeMillis();// 当前系统毫秒数
		LogUtils.i("当前系统时间戳 毫秒数==" + nowL);
		LogUtils.i("返回的时间戳秒数==" + create_time);
		if (during == 0) {
			return "不久前";
		}
		// during *= 1000;// 传过来的时间戳为妙
		// if(nowL/during>999){//穿过来的为秒
		// during*=1000;
		// }
		during = nowL - during;// 算出距离现在间隔时间 毫秒
		LogUtils.i("距离当前时间毫秒数==" + during);
		during = during / 1000 / 60;// 算出距离现在间隔时间 分钟
		LogUtils.i("距离现在间隔时间 分钟数==" + during);
		if (during <= 0) {// 一分钟前
			return "刚刚";
		} else if (during < 60) {// 由于系统时间差异
			return during + "分钟前";
		} else {
			during = during / 60;// 小时
			if (during < 24) {
				return during + "小时前";
			} else {
				during = during / 24;// 天
				if (during < 30) {
					return during + "天前";
				} else {
					during /= 30;// 月
					if (during < 12) {
						return during + "月前";
					} else {
						during /= 12;// 年
						return during + "年前";
					}
				}
			}
		}
	}

	/**
	 * @author sbk
	 * @createdate 2014-8-15 下午7:20:29
	 * @Description: 返回毫秒时间差
	 * @param time1
	 * @param time2
	 * @return
	 * 
	 */
	public static long getTimeSpace(String time1, String time2) {
		long result = 0;
		try {
			result = Long.parseLong(time2) - Long.parseLong(time1);
		} catch (Exception e) {
		}
		return result;
	}

	/*
	 * 毫秒值转换成 天时分秒
	 */
	public static String formatTime(Long ms) {
		Integer ss = 1000;
		Integer mi = ss * 60;
		Integer hh = mi * 60;
		Integer dd = hh * 24;

		Long day = ms / dd;
		Long hour = (ms - day * dd) / hh;
		Long minute = (ms - day * dd - hour * hh) / mi;
		Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

		StringBuffer sb = new StringBuffer();
		if (day > 0) {
			sb.append(day + "天");
		}
		if (hour > 0) {
			sb.append(hour + "小时");
		}
		if (minute > 0) {
			sb.append(minute + "分");
		}
		if (second > 0) {
			sb.append(second + "秒");
		}
		return sb.toString();
	}
	/**
	 * @author ty
	 * @createdate 2012-9-23 下午3:24:34
	 * @Description: 将Long值格式化
	 * @param df
	 * @param date
	 * @return
	 */
	public static String parseDateToString(DateFormat df, Date date) {
		return df.format(date);
	}

	/**
	 * 
	 * @author hxp
	 * @createdate 2014-1-16 下午4:18:55
	 * @Description: 用来处理评论时间
	 * @param time
	 * @return
	 *
	 */
	public static String commentTime(String time) {
		String[] str=time.split(" ");
		String now = parseDateToString(new SimpleDateFormat("yyyy-MM-dd"), new Date());
		if(!str[0].equals(now)){
			return time;
		}
		String[] str1 = str[0].split("-");
		String[] str2 = new String[]{
				"0","0","0"
		};
		if(str[1]!=null){
			str2=str[1].split(":");
		}
		long start=System.currentTimeMillis();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.parseInt(str1[0]), Integer.parseInt(str1[1]) - 1, Integer.parseInt(str1[2]), Integer.parseInt(str2[0]), Integer.parseInt(str2[1]));
		long end = calendar.getTimeInMillis();
		long cha = start-end;
		int day = (int) (cha / (1000 * 60 * 60 * 24));
		int hour = (int) ((cha % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
		int minute = (int) (((cha % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60))+1;
		if(hour==0){
			if(minute<=0){
				return "刚刚";
			}
			return minute+"分钟前";
		}
		return hour+"小时"+minute+"分钟前";
	}
}
